<?php
/**
 *大话水浒
 */ 
class Module_overlorduser{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	/**
	 * 游戏页面
	 * Enter description here ...
	 */
	function index(){
		$top_type = isset($_GET['type']) && in_array($_GET['type'] , array('state' , 'sidekick')) ? trim($_GET['type']) : 'state';
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		// 获取用户数据
		loadLib('User');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$key = 'user_'.$uin;
		$uin_city_key = 'city_' .$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUser($uin);
		if (empty($result)){
			return View::error('君主ID不存在.');
		}
		
		if (empty($result['Value']['task'])){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordguide&a=guide');
			break;
		}

		loadLib('Friend');
		$friend_list = Friend::get($uin);
		$friend_list[] = $uin;
//		$friend_list_info = User::getBaseInfo($friend_list);
		if ($top_type == 'state'){
			$user_lv = $result['Value']['lv'];
	
			//少于4级、取4级的可战信息
			if ($result['Value']['lv'] < 4){
				$user_lv += 1;
			}
			//查出可战人数
			$attack_user_id = App_Overlord_GameStart::getLevelUser($user_lv , 6);
			if (!empty($attack_user_id)){
				$city_user = array();
				/*
				//循环获取城池ID
				foreach ($attack_user_id as $k => $row){
					$city_user[] = 'city_' .$row;
					$fight_user[] = 'user_' .$row;
				}*/
				//查询随机攻占城池信息
				$attack_dukes_info = App_Overlord_Query::allCity($attack_user_id);
				//查询好友游戏资料
				$attack_to_user_info = App_Overlord_Query::allUser($attack_user_id);
				if (!empty($attack_to_user_info)){
					foreach ($attack_to_user_info as $k => $row){
						$k_uin = ltrim($row['Key'], 'user_');
						$attack_info[$k_uin] = $row;
					}
				}
			
				if (!empty($attack_dukes_info)){ 
					foreach ($attack_dukes_info as  $kii => $rs){
						$k_uin = ltrim($rs['Key'], 'city_');
						if ($rs['Value']['hkee']['to_uin'] != $uin){		
							$attack_user_info[$k_uin] = $attack_info[$k_uin]['Value'];
							$attack_user_info[$k_uin]['to_uin'] = $rs['Value']['hkee']['to_uin'];
							$attack_user_info[$k_uin]['generals'] = $rs['Value']['hkee']['generals'];
							if (!empty($rs['Value']['hkee']['generals_name'])){
								$attack_user_info[$k_uin]['generals_name'] = $rs['Value']['hkee']['generals_name'];
							}
						}
					}
				}
				
			}
			if (empty($attack_user_info)){
				$attack_count = 0;
				/*
				foreach ($friend_list as $k => $row){
					$city_user[] = 'city_' .$row;
					$fight_user[] = 'user_' .$row;
				}*/
				//查询好友城池信息
				$attack_dukes_info = App_Overlord_Query::allCity($friend_list);
				//查询好友游戏资料
				$attack_to_user_info = App_Overlord_Query::allUser($friend_list);
				if (!empty($attack_to_user_info)){
					foreach ($attack_to_user_info as $k => $row){
						$k_uin = ltrim($row['Key'], 'user_');
						$attack_info[$k_uin] = $row;
					}
				}
				
				if (!empty($attack_dukes_info)){ 
					foreach ($attack_dukes_info as  $k => $rs){
						$k_uin = ltrim($rs['Key'], 'city_');
						$min_lv = $result['Value']['lv'] - 4;
						$max_lv = $result['Value']['lv'] + 4;
						if ($rs['Value']['hkee']['lv'] > $min_lv && $rs['Value']['hkee']['lv'] < $max_lv && $rs['Value']['hkee']['to_uin'] != $uin){
							$attack_user_info[$k_uin] = $attack_info[$k_uin]['Value'];
							$attack_user_info[$k_uin]['to_uin'] = $rs['Value']['hkee']['to_uin'];
							$attack_user_info[$k_uin]['generals'] = $rs['Value']['hkee']['generals'];
							if (!empty($rs['Value']['hkee']['generals_name'])){
								$attack_user_info[$k_uin]['generals_name'] = $rs['Value']['hkee']['generals_name'];
							}
							$attack_count ++;
							if ($attack_count >= 6) break;
						}
					}
				}
			}
			
			//君主等级
			$user_lv = $result['Value']['lv'];
			//可攻占城池数量
			$city_count = App_Overlord_GameStart::getCityCount($user_lv);
			
			// ================================================查询主城 ===========================================
			$city_info = App_Overlord_Query::oneCity($uin);
			if (empty($city_info)){
				return View::error('城池数据异常.');
			}
			//君主可升级的等级需要的经验
			$lv = App_Overlord_GameStart::getExp($result['Value']['lv']);
			//升级
			if (!empty($lv['upgrade']) && $result['Value']['exp'] >= $lv['upgrade'] && $lv['upgrade'] != -1){
				$result['Value']['exp'] -= $lv['upgrade'];
				$result['Value']['lv'] = $lv['staff'];
				//修改君主信息
				$code = App_Overlord_Query::alterUser($uin , $result['Value']);
				
				//君主等级等于主城等级
				$city_info['Value']['hkee']['lv'] = $lv['staff'];
				//修改主城
				$code = App_Overlord_Query::alterCity($uin , $city_info['Value']);
				
				//君主升级了、改变经验
				$lv = App_Overlord_GameStart::getExp($result['Value']['lv']);
				
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlorduser');
				break;
			}
			if ($city_info['Value']['hkee']['to_uin'] == -1){
				$city_info['Value']['hkee']['name'] = '毛贼';
			}else{
				foreach ($city_info['Value'] as  $k => $row){
					if ($k == 'hkee'){
						
						$end_time = App_Overlord_GameStart::getEndDate($row['time']);
						if (!empty($end_time)){
							if ($end_time[0] != '00'){
								$city_info['Value'][$k]['end_time'] = $end_time[0] + 1 .'小时';
							}elseif($end_time[1] != '00') {
								$city_info['Value'][$k]['end_time'] = $end_time[1] + 1 .'分钟';
							}elseif ($end_time[2] != '00'){
								$city_info['Value'][$k]['end_time'] = '1分钟';
							}
						}else{
							$city_info['Value'][$k]['end_time'] = '';
						}
						if (!empty($city_info['Value'][$k]['to_uin']) && $city_info['Value'][$k]['to_uin'] != $uin){
							$info = App_Overlord_Query::oneUser($city_info['Value'][$k]['to_uin']);
							$city_info['Value'][$k]['name'] = $info['Value']['name'];
						}
					}
				}
			}
			// ==============================================查询搜索材料 ===========================================
			$build_info = App_Overlord_Query::oneCollocate('app_overlord_build', 'build_' . $uin);
			if (empty($build_info)){
				//初始化材料建筑
				$value = array('lv' => 1, 'time' => $time);
				$build_info['Value'] = $value;
				App_Overlord_Query::addCollocate('app_overlord_build', 'build_' . $uin, $value);
			}
			if (!empty($build_info['Value']['time'])){
				$end_time = App_Overlord_GameStart::getEndDate($build_info['Value']['time']);
				if (!empty($end_time)){
					if ($end_time[0] != '00'){
						$build_info['Value']['end_time'] = $end_time[0] + 1 .'小时';
					}elseif($end_time[1] != '00') {
						$build_info['Value']['end_time'] = $end_time[1] + 1 .'分钟';
					}elseif ($end_time[2] != '00'){
						$build_info['Value']['end_time'] = '1分钟';
					}
				}
			}
			
			// ==============================================查询武将资料===========================================
			$generals_info = App_Overlord_Query::oneUin_Generals($uin);
			if (!empty($generals_info['Value'])){
				foreach ($generals_info['Value'] as $k => $row){
					if ($row['state'] != 1){
						if (!empty($row['train'])){
							if ($row['train'] - $time < 0){
								if ($row['state'] == 2){
									$generals_list = $row;
									//搜索到一个就退出循环
									break;
								}
							}
						}else{
							if ($row['state'] == 3){
								$generals_list = $row;
								//搜索到一个就退出循环
								break;
							}
						}
					}
				}
			}
			
			// ============================================== 查询兵营练兵状态 =======================================
			$barracks_info = App_Overlord_Query::oneState($uin);
			if (!empty($barracks_info['Value']['train_barracks'])){
				$barracks_type = App_Overlord_GameStart::getBarracksType($result['Value']['country']);
				
				foreach ($barracks_info['Value']['train_barracks'] as $k => $row){
					if ($row['time'] - $time > 0){
						if ($k == 'infantry'){
							$barracks_id = 0;
						}elseif ($k == 'cavalryman'){
							$barracks_id = 1;
						}elseif ($k == 'toxotae'){
							$barracks_id = 2;
						}else{
							$barracks_id = 3;
						}
						$barracks_list['type'] = $k;
						$barracks_list['name'] = $barracks_type[$barracks_id][$k];
						$end_time = App_Overlord_GameStart::getEndDate($row['time']);
			
						if (!empty($end_time)){
							if ($end_time[0] != '00'){
								$barracks_list['end_time'] = $end_time[0] + 1 .'小时';
							}elseif($end_time[1] != '00') {
								$barracks_list['end_time'] = $end_time[1] + 1 .'分钟';
							}elseif ($end_time[2] != '00'){
								$barracks_list['end_time'] = '1分钟';
							}
						}else{
							$barracks_list['end_time'] = '';
						}
						break;
					}
				}
				foreach ($barracks_info['Value']['train_barracks'] as $k => $row){
					if ($row['time'] <= $time){
						$tiem_list[$k] = $row;	//看有没已练好的兵，先把数量加上
					}
				}
			}
			// =================================== 查询兵营 ====================================
			$barracks_count_info = App_Overlord_Query::oneSoldiers($uin);
			$barracks_count = 0;
	
			//计算士兵总量
			if (!empty($barracks_count_info['Value'])){
				foreach ($barracks_count_info['Value'] as $k => $row){
					$barracks_count += $row['count'];
					if (!empty($tiem_list[$row['type']])){
						$barracks_count += $tiem_list[$row['type']]['count'];  
					}
				}
			}
			// ================================== 查询动态情报 =================================
			$characters_info = App_Overlord_Query::oneCollocate('app_overlord_characters', 'characters_' .$uin);
			if (!empty($characters_info)){
				$temp_characters_info = array_reverse($characters_info['Value']);
				foreach ($temp_characters_info as $k => $row){
					$characters_list[$k] = $row;
					if ($k >= 1) break;
				}
			}
			// ================================== 查询系统播报=================================
			$system_info = App_Overlord_Query::oneCollocate('app_overlord_system', 'system_' .$uin);
			if (!empty($system_info)){
				$temp_system_info = array_reverse($system_info['Value']);
				foreach ($temp_system_info as $k => $row){
					$system_list[$k] = $row;
					if ($k >= 1) break;
				}
			}
			//看是否已使用休战符
			if (!empty($result['Value']['cease']) && $result['Value']['cease'] - $time > 0){
				$result['Value']['cease_time'] = App_Overlord_GameStart::getEndDate($result['Value']['cease']);
			}
			
			//初始攻占城池数量
			$generals_city_count = 0;
			if (!empty($generals_info['Value'])){
				foreach ($generals_info['Value'] as $k => $row){
					if (!empty($row['to_uin']) && $row['to_uin'] != $uin){
						$generals_city_count += 1;
					}
				}
			}
			
			if (!empty($generals_list)){
				$smarty->assign('generals_list' , $generals_list);
			}
			if (!empty($barracks_list)){
				$smarty->assign('barracks_list' , $barracks_list);
			}
			if (!empty($system_list)){
				$smarty->assign('system_list' , $system_list);
			}
			if (!empty($characters_list)){
				$smarty->assign('characters_list' , $characters_list);
			}
			//可战城池
			if (!empty($attack_user_info)){
				$smarty->assign('attack_user_info' , $attack_user_info);
			}
			//可招武将
			$lv_generals = App_Overlord_GameStart::getGeneralsLv($result['Value']['lv']);
			//将领总数
			$generals_count = count($generals_info['Value']);
			if (empty($lv['upgrade'])){
				$lv['upgrade'] = -1;
			}
			//兵营
			$smarty->assign('barracks_count' , $barracks_count);
			$smarty->assign('exp' , $lv['upgrade']);
			$smarty->assign('city_count' , $city_count);
			$smarty->assign('lv_generals' , $lv_generals);
			$smarty->assign('lv_generals_count' , $generals_count);
			$smarty->assign('city_info' , $city_info['Value']);
			$smarty->assign('generals_city_count' , $generals_city_count);
			$smarty->assign('build_info' , $build_info['Value']);
		}elseif ($top_type == 'sidekick'){
			$top = isset($_GET['top']) && in_array($_GET['top'] , array('lv' , 'occupy')) ? trim($_GET['top']) : 'occupy';
			$page = isset($_GET['page']) ? intval($_GET['page']) : null;
			
			//设置每行10条数据
			$per_page = 10;
			if($page <= 0){
				$page = 1;
			}
			/*
			foreach ($friend_list as $k => $row){
				$user_key[$k] = 'user_'.$row;
				$city_key[$k] = 'city_'.$row;
				
			}*/
	
			//查询好友君主资料
			$user_in_info = App_Overlord_Query::allUser($friend_list);	
	
			if (!empty($user_in_info)){
				foreach ($user_in_info as $k => $row){
					$user_in_key = ltrim($row['Key'] , 'user_');
					$row['Key'] = $user_in_key;
					$user_in_array[$user_in_key] = $row;
				}
			}
			
	//		print_r($user_in_info);
			$city_in_info = App_Overlord_Query::allCity($friend_list);
			if (!empty($city_in_info)){
				foreach ($city_in_info as $k => $row){
					$city_in_key = ltrim($row['Key'] , 'city_');
					$user_in_array[$city_in_key]['Value']['city'] = $row['Value'];
				
					$to_city_uin = 'city_' . $row['Value']['hkee']['to_uin'];
					if (!empty($row['Value']['hkee']['to_uin']) && $row['Value']['hkee']['to_uin'] != $uin && $to_city_uin != $row['Key']){
						$to_uin_array[] = $row['Value']['hkee']['to_uin'];
					}
				}
			}
	
			//城池被占领，查询对方
			if (!empty($to_uin_array)){
				//查询君主被攻占人资料
				$user_array_info = App_Overlord_Query::allUser($to_uin_array);		
				foreach ($user_array_info as $k => $row){
					$fight_key = ltrim($row['Key'] , 'user_');
					$user_fight_info[$fight_key] = $row['Value'];
				}
			}
			if (!empty($user_fight_info)){
				foreach ($city_in_info as $k => $row){
					$user_to_in_key = ltrim($row['Key'] , 'city_');
					if (!empty($row['Value']['hkee']['to_uin']) && $row['Value']['hkee']['to_uin'] != $uin && $to_city_uin != $row['Key'] && !empty($user_fight_info[$row['Value']['hkee']['to_uin']])){
						$user_in_array[$user_to_in_key]['Value']['city']['hkee']['name'] = $user_fight_info[$row['Value']['hkee']['to_uin']]['name'];
						$user_in_array[$user_to_in_key]['Value']['city']['hkee']['to_lv'] = $user_fight_info[$row['Value']['hkee']['to_uin']]['lv'];
					}
				}
			}
			
			if (!empty($user_in_array)){
				foreach ($user_in_array as $k => $row){
					$friend[] = $user_in_array[$k];
				}
			}
			
			if (!empty($friend)){
				//君主排名
				$list = App_Overlord_GameStart::getArrSort($friend);
				$count = count($list);
				if ($top == 'occupy'){
					$count -= 1;
				}
				/*******************数据分页******************/
				$page = ($page-1) * $per_page;
				$is_list = array_slice($list,$page,$per_page);
	//	print_r($is_list);
				$smarty->assign('friend' , $is_list);
				$smarty->assign('per_page',$per_page);
				$smarty->assign('count',$count);
			}
			$smarty->assign('top' , $top);
		}
		//新手礼包信息
		if (!empty($result['Value']['new'])){
			$end_time_array = App_Overlord_GameStart::getEndDate($result['Value']['new']['time']);
			if (!empty($end_time_array)){
				if ($end_time_array[0] != '00'){
					$result['Value']['new']['time'] = $end_time_array[0] + 1 .'小时';
				}elseif($end_time_array[1] != '00') {
					$result['Value']['new']['time'] = $end_time_array[1] + 1 .'分钟';
				}elseif ($end_time_array[2] != '00'){
					$result['Value']['new']['time'] = '1分钟';
				}
				
			}else{
				$result['Value']['new']['time'] = '';
			}
		}
		
		$smarty->assign('list' , $result['Value']);
		$smarty->assign('top_type' , $top_type);
		$smarty->assign('key' , $uin);
		return $smarty->fetch('app/Overlord/index.tpl.php');
	}
	
	/**
	 * 铁匠铺
	 * Enter description here ...
	 */
	function smithy(){
		$top_type = isset($_GET['type']) && in_array($_GET['type'] , array('strengthen' , 'mending')) ? trim($_GET['type']) : 'strengthen';
		$article_type = isset($_GET['article_type']) && in_array($_GET['article_type'] , array('weapon' , 'armour' , 'helm', 'horse')) ? trim($_GET['article_type']) : 'weapon';
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		if($page <= 0){
			$page = 1;
		}
		$count = 0;
		//控制显示数据数量
		$per_page = 10;
		/*******************数据分页******************/
		$offset = ($page-1) * $per_page;
		
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		//物品配置
		loadLib('App_Overlord_Collocate');
		//查询物品内容
		$data = App_Overlord_Collocate::getGoods();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		if (!empty($items_info['Value']['equip'][$article_type])){
			foreach ($items_info['Value']['equip'][$article_type] as $k => $row){
				$items_info['Value']['equip'][$article_type][$k]['name'] = $data[$article_type][$row['id']]['name'];
				if ($row['state'] == 0){
					$count += 1;
				}
			}
			$smarty->assign('list',$items_info['Value']['equip'][$article_type]);
		}
		$smarty->assign('per_page',$per_page);
		$smarty->assign('offset',$offset);
		$smarty->assign('count',$count);
		$smarty->assign('top_type',$top_type);
		$smarty->assign('article_type',$article_type);
		return $smarty->fetch('app/Overlord/smithy.tpl.php');
	}
	/**
	 * 强化页面
	 */
	function strengthen_page(){
		$article_type = isset($_GET['article_type']) ? trim($_GET['article_type']) : '';
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		// ============================= 判断post传过来的值是否正确=====================
		if(empty($article_type) || empty($id)) {
			return View::error('参数错误.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		if (empty($items_info) || empty($items_info['Value']['equip'][$article_type])){
			return View::error('数据异常');
		}
		if (empty($items_info['Value']['equip'][$article_type][$id]) || $items_info['Value']['equip'][$article_type][$id]['state'] == 1){
			return View::error('该装备不存在.');
		}
		//装备信息
		$equip_info = $items_info['Value']['equip'][$article_type][$id];
		if ($equip_info['lv'] >= 7){
			return View::error('你的装备已经是最高级,无需升级.');
		}
		//物品配置
		loadLib('App_Overlord_Collocate');
		//查询物品内容
		$data = App_Overlord_Collocate::getGoods();
		//装备升级所需要的信息
		$equip_strengthen = App_Overlord_Collocate::getMaterialLv($equip_info['lv']);
		
		//升级装备必备材料ID
		$public_id = 0; 
		if ($article_type == 'weapon'){
			//升级武器材料ID
			$material_id = 3;
		}elseif ($article_type == 'armour'){
			//升级盔甲材料ID
			$material_id = 2;
		}elseif ($article_type == 'helm'){
			//升级头盔材料ID
			$material_id = 1;
		}elseif ($article_type == 'horse'){
			//升级战马材料ID
			$material_id = 4;
		}
		//材料计数
		if (!empty($items_info['Value']['material'])){
			foreach ($items_info['Value']['material'] as $k => $row){
				if (!empty($row['count'])){
					$list[$k]['name'] = $data['material'][$k]['name'];
					$list[$k]['count'] = $row['count'];
				}
			}
					
			if (!empty($list)){
				ksort($list); 
				$smarty->assign('list',$list);
			}
		}
		//装备名称
		$name = $data[$article_type][$equip_info['id']]['name'];
		//材料名称
		$public_name = $data['material'][$public_id]['name'];
		$material_name = $data['material'][$material_id]['name'];
		$public = array();
		$public_num = array();
		$public[$public_id] = $public_name;
		$public[$material_id] = $material_name;
		$public_num[$public_id] = $equip_strengthen['upgrade'][0];
		$public_num[$material_id] = $equip_strengthen['upgrade'][1];
		
		
		$smarty->assign('article_type',$article_type);
		$smarty->assign('id',$id);
		$smarty->assign('public',$public);
		$smarty->assign('public_num',$public_num);
		$smarty->assign('name',$name);
		$smarty->assign('lv',$equip_info['lv']);
		$smarty->assign('strengthen',$equip_strengthen);
		return $smarty->fetch('app/Overlord/strengthen_page.tpl.php');
	}
	/**
	 * 强化
	 */
	function strengthen(){
		$article_type = isset($_POST['article_type']) ? trim($_POST['article_type']) : '';
		$id = isset($_POST['id']) ? trim($_POST['id']) : '';
		$uin = SessionInfo('uin');
		// ============================= 判断post传过来的值是否正确=====================
		if(empty($article_type) || empty($id)) {
			return View::error('参数错误.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		if (empty($items_info) || empty($items_info['Value']['equip'][$article_type])){
			return View::error('数据异常');
		}
		if (empty($items_info['Value']['equip'][$article_type][$id]) || $items_info['Value']['equip'][$article_type][$id]['state'] == 1){
			return View::error('该装备不存在.');
		}
		
		//装备信息
		$equip_info = $items_info['Value']['equip'][$article_type][$id];
		if ($equip_info['lv'] >= 7){
			return View::error('你的装备已经是最高级,无需升级.');
		}
		//物品配置
		loadLib('App_Overlord_Collocate');
		//查询物品内容
		$data = App_Overlord_Collocate::getGoods();
		//装备升级所需要的信息
		$equip_strengthen = App_Overlord_Collocate::getMaterialLv($equip_info['lv']);
		//获取任务类型配置
		loadLib('App_Overlord_Task');
		$task_type = App_Overlord_Task::getTaskType();
		
		//升级装备必备材料ID
		$public_id = 0; 
		if ($article_type == 'weapon'){
			//升级武器材料ID
			$material_id = 3;
		}elseif ($article_type == 'armour'){
			//升级盔甲材料ID
			$material_id = 2;
		}elseif ($article_type == 'helm'){
			//升级头盔材料ID
			$material_id = 1;
		}elseif ($article_type == 'horse'){
			//升级战马材料ID
			$material_id = 4;
		}
		//材料名称
		$public_name = $data['material'][$public_id]['name'];
		$material_name = $data['material'][$material_id]['name'];
		if (empty($items_info['Value']['material'][$public_id]['count'])){
			$items_info['Value']['material'][$public_id]['count'] = 0;
		}
		//升级的主材料
		$items_info['Value']['material'][$public_id]['count'] -= $equip_strengthen['upgrade'][0];
		if ($items_info['Value']['material'][$public_id]['count'] < 0){
			return View::error('你缺少的【'. $public_name .'】'. $items_info['Value']['material'][$public_id]['count'] * -1 .'个' , 0 , array(
					array('name' => '去商城购买' , 'url' => '/app.php?m=overlordcommodity&amp;a=mall&amp;type=diamond&amp;article_type=rest&amp;sid=' . sessionInfo('skey')),
					));
		}
		if (empty($items_info['Value']['material'][$material_id]['count'])){
			$items_info['Value']['material'][$material_id]['count'] = 0;
		}
		//升级辅材料
		$items_info['Value']['material'][$material_id]['count'] -= $equip_strengthen['upgrade'][1];
		if ($items_info['Value']['material'][$material_id]['count'] < 0){
			return View::error('你缺少的【'. $material_name .'】'. $items_info['Value']['material'][$material_id]['count'] * -1 .'个' , 0 , array(
					array('name' => '去商城购买' , 'url' => '/app.php?m=overlordcommodity&amp;a=mall&amp;type=exploits&amp;article_type=rest&amp;sid=' . sessionInfo('skey')),
					));
		}
		//当升级失败满5次、给成功
		if ($equip_info['upgrade'] >= 5){
			//成功升级
			$items_info['Value']['equip'][$article_type][$id]['lv'] = $equip_strengthen['staff'];
			//计数重新归零
			$items_info['Value']['equip'][$article_type][$id]['upgrade'] = 0;
			$rand = 1;
		}else{
			//升级概率
			$rand = App_Overlord_GameStart::getChance($equip_strengthen['odds']);
			if (empty($rand)){
				//失败计数加1
				$items_info['Value']['equip'][$article_type][$id]['upgrade'] += 1;
				$rand = 0;
			}else{
				//升级成功.
				$items_info['Value']['equip'][$article_type][$id]['lv'] = $equip_strengthen['staff'];
			}
		}
		if (!empty($rand)){
			//武器等级
			$lv = $items_info['Value']['equip'][$article_type][$id]['lv'];
			if ($article_type == 'weapon'){
				if ($lv == 1){
					//完成强化武器任务
					App_Overlord_Task::getGo('main' , $task_type['main']['weapon']);
				}
			}elseif ($article_type == 'armour'){
				if ($lv == 1){
				//完成强化盔甲任务
				App_Overlord_Task::getGo('main' , $task_type['main']['armour']);
				}
			}elseif ($article_type == 'helm'){
				if ($lv == 1){
					//完成强化头盔任务
					App_Overlord_Task::getGo('main' , $task_type['main']['helm']);
				}
			}elseif ($article_type == 'horse'){
				if ($lv == 1){
					//完成强化战马任务
					App_Overlord_Task::getGo('main' , $task_type['main']['horse']);
				}
			}
			if ($lv > 1){
				//装备升级任务
				App_Overlord_Task::trigger('equip' , array('lv' => $lv));
			}
		}
		//更新背包信息
		App_Overlord_Query::alterItems($uin, $items_info['Value']);
		
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=strengthen&id='.$id.'&article_type='.$article_type.'&equip_id='.$equip_info['id'].'&rand='.$rand.'&staff='.$equip_strengthen['staff']);
	}
	
	/**
	 * 功能建筑
	 * Enter description here ...
	 */
	function exploit_build(){
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		
		//加载物品
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_Query');
		//功勋建筑
		$data = App_Overlord_Collocate::getExploitMaterial();
		//加载数据连接
		loadLib('App_Overlord_GameStart');
		//取得建筑加成
		$count = 0;
		//获取建筑表
		$exploit_info = App_Overlord_Query::oneExploit_Build($uin);
		if (!empty($exploit_info)){
			foreach ($exploit_info['Value'] as $k => $row){
				$exploit_info['Value'][$k]['name'] = $data[$k]['name'];
				//建筑数量为0不计算
				if (!empty($row['count'])){
					$count += $data[$k]['num'] * $row['count'];
				}
			}
			$smarty->assign('list',$exploit_info['Value']);
		}
	
		$smarty->assign('count',$count);
		return $smarty->fetch('app/Overlord/exploit_build.tpl.php');
	}
	/**
	 * 建设介绍页面
	 */
	function build_page(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		
		//加载物品
		loadLib('App_Overlord_Collocate');
		
		
		//功勋建筑
		$data = App_Overlord_Collocate::getExploitMaterial();
		if (empty($data[$id])) {
			return View::error('数据异常');
		}
		//取需建设的信息
		$data_info = $data[$id];
		$smarty = loadSmarty();
		$smarty->assign('list',$data_info);
		return $smarty->fetch('app/Overlord/build_page.tpl.php');
	}
	/**
	 * 进入建设页面
	 * Enter description here ...
	 */
	function exploit_build_page(){
		$uin = SessionInfo('uin');
		//加载物品
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_Query');
		//加载数据连接
		loadLib('App_Overlord_GameStart');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)) {
			return View::error('获取君主资料失败.');
		}
		//功勋建筑
		$data = App_Overlord_Collocate::getExploitMaterial();

		$smarty = loadSmarty();
		$smarty->assign('data' , $data);
		$smarty->assign('list' , $user_info['Value']);
		return $smarty->fetch('app/Overlord/exploit_build_page.tpl.php');
	}
	/**
	 * 开始建设
	 */
	function build(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
		$uin = SessionInfo('uin');
		//加载物品
		loadLib('App_Overlord_Collocate');
		//加载数据连接
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)) {
			return View::error('获取君主资料失败.');
		}
		//功勋建筑
		$data = App_Overlord_Collocate::getExploitMaterial();
		if (empty($data[$id])) {
			return View::error('数据异常');
		}
		//取需建设的信息
		$data_info = $data[$id];
		if ($user_info['Value']['lv'] < $data_info['lv']) {
			return View::error('你的等级不足，不能修建.' , 0 , array(
						array('name' => '去攻城' , 'url' => '/app.php?m=overlordcity&amp;a=battlecity&amp;sid=' . sessionInfo('skey')),
						array('name' => '去做任务' , 'url' => '/app.php?m=overlordtask&amp;sid=' . sessionInfo('skey')),
					));
		}
		if ($user_info['Value']['exploits'] < $data_info['exploit']){
			return View::error('你的功勋不足，不能修建<br />战斗胜利可获得功勋<br />' , 0 , array(
						array('name' => '去攻城' , 'url' => '/app.php?m=overlordcity&amp;a=battlecity&amp;sid=' . sessionInfo('skey')),
					));
		}
		$user_info['Value']['exploits'] -= $data_info['exploit'];
		//获取任务类型
		loadLib('App_Overlord_Task');
		$task_type = App_Overlord_Task::getTaskType();
		//完成建设建筑任务
		App_Overlord_Task::getGo('main' , $task_type['main']['build']);
		//修改君主信息
		App_Overlord_Query::alterUser($uin, $user_info['Value']);//查询建筑
		$exploit_info = App_Overlord_Query::oneExploit_Build($uin);
		if (empty($exploit_info)){
			$exploit_info['Value'][$id]['count'] = 1; 
			App_Overlord_Query::addExploit_Build($uin , $exploit_info['Value']);
		}else{
			if (empty($exploit_info['Value'][$id])){
				$exploit_info['Value'][$id]['count'] = 1; 
			}else{
				$exploit_info['Value'][$id]['count'] += 1; 
			}
			App_Overlord_Query::alterExploit_Build($uin , $exploit_info['Value']);
		}
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=build&id=1&type_id='.$id);
	}
	/**
	 * 消息
	 */
	function information(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$type = isset($_GET['type']) && in_array($_GET['type'] , array('characters' , 'trends' , 'consumption')) ? trim($_GET['type']) : 'characters';
		$smarty = loadSmarty();
		$uin = SessionInfo('uin');
		//设置每行10条数据
		$per_page = 5;
		if($page <= 0){
			$page = 1;
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$count = 0;
		if ($type == 'characters'){
			//查询攻击记录
			$characters_info = App_Overlord_Query::oneCollocate('app_overlord_characters', 'characters_' .$uin);
/*		}elseif ($type == 'trends'){
			//查询动态记录
			$trends_info = App_Overlord_GameStart::getOneOverlord('app_overlord_trends', 'trends_' .$uin);*/
		}elseif ($type == 'consumption'){
			//查询消费记录
			$consume_info = App_Overlord_Query::oneConsume($uin);
		}
		
		/*******************数据分页******************/
		//情报
		$page = ($page-1) * $per_page;
		if (!empty($characters_info)){
			$count = count($characters_info['Value']);
			$list = array_reverse($characters_info['Value']);
			$list = array_slice($list,$page,$per_page);
		}
	/*	
		//动态
		if (!empty($trends_info)){
			loadLib('App_Overlord_Collocate');
			$data = App_Overlord_Collocate::getGoods();
			if (!empty($trends_info['Value'])){
				foreach ($trends_info['Value'] as $k => $row){
					if (!empty($row['material'])){
						foreach ($row['material'] as $g => $rs){
							$trends_info['Value'][$k]['material'][$g] = $data['material'][$g]['name'];
							$trends_info['Value'][$k]['count'][$g] = $rs;
						}
					}
				}
			}
			$count = count($trends_info['Value']);
			$list = array_reverse($trends_info['Value']);
			$list = array_slice($list , $page , $per_page);
		}*/
		
		//消费
		if (!empty($consume_info)){
			$count = count($consume_info['Value']);
			$list = array_reverse($consume_info['Value']);
			$list = array_slice($list,$page,$per_page);
		}
		if (!empty($list)){
			$smarty->assign('list',$list);
		}
	
		$smarty->assign('per_page',$per_page);
		$smarty->assign('count',$count);
		$smarty->assign('type',$type);
		return $smarty->fetch('app/Overlord/information.tpl.php');
	}
	
	
	/**
	 * 等级
	 */
	function userlv(){
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_Overlord_Query');
		//查询君主资料
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主等级失败!');
		}
		
		$smarty->assign('list',$user_info['Value']);
		return $smarty->fetch('app/Overlord/userlv.tpl.php');
	}
	/**
	 * 武将
	 */
	function generals_count(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_Overlord_Query');
		loadLib('App_Overlord_GameStart');
		//查询君主资料
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主等级失败!');
		}
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$count = 0;
		//查询个人武将
		$generals_result = App_Overlord_Query::oneUin_Generals($uin);
		$count = count($generals_result['Value']);
		
		/*******************数据分页******************/
		$page = ($page-1) * $per_page;
		if (!empty($generals_result['Value'])){
			foreach ($generals_result['Value'] as $k => $row){
				$generals_result['Value'][$k]['id'] = $k;
				//武将升级所需的经验
				$generals_lv = App_Overlord_GameStart::getGeneralsExp($row['lv']);
				$generals_result['Value'][$k]['upgrade'] = $generals_lv['upgrade'];
				if ($row['exp'] >= $generals_lv['upgrade']){
					//武将升级
					$info = App_Overlord_GameStart::getGeneralsPromotion($generals_result['Value'][$k], $generals_lv);
					$generals_result['Value'][$k] = $info;
					$statelv = 1;
				}
				$generals_array[] = $generals_result['Value'][$k]; 
			}
			if (!empty($statelv)){
				//修改武将
				$code = App_Overlord_Query::alterUin_Generals($uin , $generals_result['Value']);
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlorduser&a=generals_count');
			}
			$generals_max_lv = App_Overlord_GameStart::getGeneralsArrSort($generals_array);
			$list = array_slice($generals_max_lv,$page,$per_page);
			$smarty->assign('list',$list);
		}
		//可招募将领数量
		$generals_count = App_Overlord_GameStart::getGeneralsLv($user_info['Value']['lv']);
		$smarty->assign('generals_count',$generals_count);
		$smarty->assign('count',$count);
		$smarty->assign('per_page',$per_page);
		return $smarty->fetch('app/Overlord/generals_count.tpl.php');
	}
	/**
	 * 游戏帮助
	 */
	function faqindex(){
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		$issue = App_Overlord_GameStart::getFaq();
		$smarty->assign('list' , $issue);
		return $smarty->fetch('app/Overlord/faqindex.tpl.php');
	}
	/**
	 * 进入问题界面
	 */
	function faqpege(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		loadLib('App_Overlord_GameStart');
		$issue = App_Overlord_GameStart::getFaq();
		if (empty($issue[$id])){
			return View::error('无此问题!');
		}
		$particular = App_Overlord_GameStart::getIssue();
		if (empty($particular)) {
			return View::error('没有该项问题!');
		}
		
		$smarty = loadSmarty();
		$smarty->assign('issue' , $issue);
		$smarty->assign('id' , $id);
		$smarty->assign('list' , $particular);
		return $smarty->fetch('app/Overlord/faqpege.tpl.php');
	}
	/**
	 * 详细问题解答
	 */
	function faq(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$faqid = isset($_GET['faqid']) ? intval($_GET['faqid']) : 0;
		loadLib('App_Overlord_GameStart');
		$issue = App_Overlord_GameStart::getFaq();
		if (empty($issue[$id])){
			return View::error('无此问题!');
		}
		$particular = App_Overlord_GameStart::getIssue();
		if (empty($particular[$id])) {
			return View::error('没有该项问题!');
		}
		$smarty = loadSmarty();
		$smarty->assign('issue' , $issue);
		$smarty->assign('id' , $id);
		$smarty->assign('faqid' , $faqid);
		$smarty->assign('list' , $particular);
		return $smarty->fetch('app/Overlord/faq.tpl.php');
	}
	/**
	 * 充值界面
	 */
	function pay(){
		$smarty = loadSmarty();
		//用于数据库分表

		loadLib('App_Overlord_Collocate');
/*
		$data = '';
		$type = "'award'";
		$lv = "'gold'";
		$f = "'recruit'";
		$g = "'material'";
		$d = "'next'";
		$v = 95;
		$gold = 12000;
		for ($i = 13 ; $i < 76 ; $i ++){
			$ll = $i % 2;
			if (!empty($ll)){
			$k = $i + 2;
			$gold += 2000;
			$data[] = 'case '.$i.': $award = array('.$type.' => array('.$lv.' => '.$gold.' , '.$f.' => array(0 , 2) , '.$g.' => array(0 , 2)),
	'.$d.' => '.$k.',
	);break;'
			;
			}
		}
		$a = "'0'";
		for ($i = 1 ; $i <= 64 ; $i++){	
			$data[] = 'CREATE TABLE `soldiers_' .$i. '` (
			  `Key` varchar(100) NOT NULL,
			  `Value` text NOT NULL,
			  `time` int(11) NOT NULL default '.$a.',
			  PRIMARY KEY  (`Key`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8;';
		}
		$strf = implode($data , "\r\n");	
			
		//将内容写入数据库	
		@$fp = fopen("xx.txt","w"); 
		if(!$fp){ 
			echo "system error"; 
			exit(); 
		}else { 
		
		fwrite($fp,$strf); 
		fclose($fp); 
		}
*/
		return $smarty->fetch('app/Overlord/pay.tpl.php');
	}
}
# end of script